package ciyuan.Item.SkillBook;

import ciyuan.Item.ItemGroups;
import ciyuan.Item.ItemRegisterManager;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.AbstractArrowEntity;
import net.minecraft.entity.projectile.ArrowEntity;
import net.minecraft.item.ArrowItem;
import net.minecraft.item.CrossbowItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.Hand;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class Impact extends SkillBookType {

    private static final String ARROW_IS_KNOCKBACK = ",ARROW_IS_KNOCKBACK";

    public Impact() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        SkillUtils.setSkillColdInterval(playerEntity, this, 14 * 1000);

        ItemStack itemStack = playerEntity.getItemInHand(Hand.MAIN_HAND);
        if (itemStack.isEmpty()) {
            return;
        }

        if (itemStack.getItem() instanceof CrossbowItem) {
            playerEntity.knockback(2F, -MathHelper.sin(playerEntity.yRot * ((float) Math.PI / 180F)), -MathHelper.cos(playerEntity.yRot * ((float) Math.PI / 180F)));
            addChargedProjectile(itemStack, new ItemStack(ItemRegisterManager.jian1.get()));
            setCharged(itemStack,true);
        }

        SkillUtils.saveSkillCastTime(playerEntity,this);
    }

    private static void setCharged(ItemStack p_220011_0_, boolean p_220011_1_) {
        CompoundNBT compoundnbt = p_220011_0_.getOrCreateTag();
        compoundnbt.putBoolean("Charged", p_220011_1_);
    }

    private void addChargedProjectile(ItemStack p_220029_0_, ItemStack p_220029_1_) {
        CompoundNBT compoundnbt = p_220029_0_.getOrCreateTag();
        ListNBT listnbt;
        listnbt = new ListNBT();

        CompoundNBT compoundnbt1 = new CompoundNBT();
        p_220029_1_.save(compoundnbt1);
        listnbt.add(compoundnbt1);
        compoundnbt.put("ChargedProjectiles", listnbt);
    }

    @Override
    public String getSkillCastName() {
        return "Impact";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.18");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.18");
    }

    public static class MyArrow extends ArrowItem {
        public MyArrow() {
            super(new Properties());
        }

        @Override
        public AbstractArrowEntity createArrow(World p_200887_1_, ItemStack p_200887_2_, LivingEntity p_200887_3_) {
            ArrowEntity arrowentity = new MyArrowentity(p_200887_1_, p_200887_3_);
            arrowentity.setEffectsFromItem(p_200887_2_);
            arrowentity.setKnockback(2);
            arrowentity.setPierceLevel((byte) 10);
            return arrowentity;
        }

        public static class MyArrowentity extends ArrowEntity {

            public MyArrowentity(World p_i46758_1_, LivingEntity p_i46758_2_) {
                super(p_i46758_1_, p_i46758_2_);
            }

            @Override
            protected ItemStack getPickupItem() {
                return ItemStack.EMPTY;
            }

            @Override
            public int getColor() {
                return 16284963;
            }
        }

    }
}
